function getParentTR(dom) {
    if (dom) {
        if (dom.tagName.toLowerCase() == 'tr') {
            return dom;
        } else {
            return getParentTR(dom.parentNode);
        }
    } else {
        return null;
    }
}
function RepeateTableOperator(parentDom) {

    var operateDiv = null;

    var eventTR = null;

    var afterAddTrOperate = null;
    var beforeDelTrOperate;

    var trParent = parentDom;

    var allowAdd = true;
    var allowDel = true;

    /**
     * 获取dom坐标
     * 
     * @param obj
     * @return {x:100,y:200}
     */
    function getElemPos(obj) {
        var pos = {"top": 0, "left": 0};
        if (obj.offsetParent) {
            while (obj.offsetParent) {
                pos.top += obj.offsetTop;
                pos.left += obj.offsetLeft;
                obj = obj.offsetParent;
            }
        } else if (obj.x) {
            pos.left += obj.x;
        } else if (obj.x) {
            pos.top += obj.y;
        }
        return {
            x: pos.left,
            y: pos.top
        };
    }

    /**
     * 插入dom
     * 
     * @param newEl
     * @param targetEl
     */
    function insertAfter(newEl, targetEl) {
        var parentEl = targetEl.parentNode;
        if (parentEl.lastChild == targetEl) {
            parentEl.appendChild(newEl);
        } else {
            parentEl.insertBefore(newEl, targetEl.nextSibling);
        }
    }
    /**
     * 清空dom的值
     * 
     * @param dom
     */
    function clearValue(dom) {
        var childNodes = dom.childNodes;
        if (childNodes) {
            for (var i = 0, length = childNodes.length; i < length; i++) {
                if (childNodes[i].tagName
                        && childNodes[i].tagName.toUpperCase() === 'INPUT') {
                    childNodes[i].value = '';
                } else {
                    clearValue(childNodes[i]);
                }
            }
        }
    }

    /**
     * 添加重复表的行
     * 
     * @param tr
     */
    function addRepeatRow(tr) {
        var cloneTr = tr.cloneNode(true);
        clearValue(cloneTr);
        insertAfter(cloneTr, tr);
        cloneTr.onclick = showOperateDiv;

        if (afterAddTrOperate && typeof afterAddTrOperate == "function") {
            afterAddTrOperate(cloneTr);
        }
        // cloneTr.onmouseout = onMouseOut;
    }

    function createOperateDiv() {

        // <div id='repeatOperatorDiv'
        // style="width: 13px; display: none; position: absolute; z-index:
        // 10000; left: 0; top: 0;">
        // <img id='addImg' src="./apps_res/oa2uap/img/add.gif" width="13"
        // height="13" /><img id='deleteImg'
        // src="./apps_res/oa2uap/img/delete.gif" width="13" height="13" />
        // </div>
        operateDiv = document.createElement('div');
        operateDiv.className = 'repeatOperateDiv';
        // operateDiv.style.display = 'none';

        addDIV = document.createElement('div');
        addDIV.className = 'addOperateImg';
        addDIV.onclick = function () {
            if (allowAdd)
                addTr();
        };
        operateDiv.appendChild(addDIV);

        delDIV = document.createElement('div');
        delDIV.className = 'delOperateImg';
        delDIV.onclick = function () {
            if (allowDel)
                delTR();
        };
        operateDiv.appendChild(delDIV);

        document.body.appendChild(operateDiv);
    }

    /**
     * 当鼠标在tr上面时
     */
    function showOperateDiv() {
        event.cancelBubble = true;
        if (allowAdd && allowDel) {
            if (!operateDiv) {
                createOperateDiv();
            }
            eventTR = getParentTR(this);
            var pos = getElemPos(eventTR);
            operateDiv.style.left = pos.x - 15;
            operateDiv.style.top = pos.y;
            operateDiv.style.display = '';
        }
    }

    function hideOperateDiv() {
        event.cancelBubble = true;
        if (operateDiv) {
            operateDiv.style.display = 'none';
        }
    }
    this.hideOperateDiv1 = function () {
        if (operateDiv) {
            operateDiv.style.display = 'none';
        }
    }

    function fun_name(num) {
        if (num != null && num != undefined) {
            var tmp = num.toString();
            var re = /function\s*(\w*)/i;
            var matches = re.exec(tmp);
            return matches[1];
        } else {
            return null;
        }
    }

    function addTr() {
        event.cancelBubble = true;
        if (operateDiv) {
            operateDiv.style.display = 'none';
        }

        if (eventTR) {
            addRepeatRow(eventTR);
        }
        eventTR = null;
    }

    function delTR() {
        event.cancelBubble = true;
        if (operateDiv) {
            operateDiv.style.display = 'none';
        }

        if (eventTR) {
            if (beforeDelTrOperate) {
                if (beforeDelTrOperate(eventTR) === false) {
                    return;
                }
            }

            var children = eventTR.parentNode.childNodes;
            var count = 0;

            for (var i = 0; i < children.length; i++) {
                if (fun_name(children[i].onclick) == 'showOperateDiv') {
                    count++;
                }
                if (count > 1) {
                    break;
                }
            }

            if (count > 1) {
                eventTR.parentNode.removeChild(eventTR);
            } else {
                alert('不能再删除了');
            }
        }
        eventTR = null;
    }

    this.addEmptyTR = function (tr, number) {
        if (!isNaN(number)) {
            for (var i = 0; i < number; i++) {
                addRepeatRow(tr);
            }
        } else {
            addRepeatRow(tr);
        }
    };

    this.addListeners = function (dom) {
        getParentTR(dom).onclick = showOperateDiv;
    };

    this.setAllowAdd = function (allow) {
        allowAdd = allow;
    };

    this.setAfterAddTrOperate = function (afterAddTrOperate1) {
        afterAddTrOperate = afterAddTrOperate1;
    };

    this.setBeforeDelTrOperate = function (beforeDelTrOperate1) {
        beforeDelTrOperate = beforeDelTrOperate1;
    }

    this.setAllowDel = function (allow) {
        allowDel = allow;
    };

    this.clearAllRepeatTR = function () {
        var children = eventTR.parentNode.childNodes;
        var repeatTRArray = [];

        for (var i = 0; i < children.length; i++) {
            if (fun_name(children[i].onclick) == 'showOperateDiv') {
                repeatTRArray.push(children[i]);
            }
        }
        if (repeatTRArray.length > 0) {
            for (var i = 0; i < repeatTRArray.length - 1; i++) {
                document.removeChild(repeatTRArray[i]);
            }

            clearValue(repeatTRArray[repeatTRArray.length - 1]);
        }
        repeatTRArray = null;
    };
}
